//
//  RHEditUserInfo.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/10/29.
//

#import "RHEditUserInfoVC.h"

@interface RHEditUserInfoVC ()

@property (nonatomic, strong) QMUITextField *nickTF;
@end

@implementation RHEditUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    self.navigationItem.title = @"备注下级用户名称".rh_localized;
    [self setupUI];
}

-(void)setupUI
{
    UIView *topView = kView;
    topView.layer.cornerRadius = 8;
    topView.layer.masksToBounds = YES;
    topView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(15);
        make.right.mas_offset(-15);
    }];
    
    UIView *oriView = [self itemViewWithTitle:[NSString stringWithFormat:@"%@:",@"原用户名".rh_localized] content:self.usermodel.userName showLine:YES canEdit:NO];
    [topView addSubview:oriView];
    [oriView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(0);
        make.height.offset(45);
    }];
    
    UIView *nickView = [self itemViewWithTitle:[NSString stringWithFormat:@"%@:",@"备注用户名".rh_localized] content:self.usermodel.remarkName showLine:NO canEdit:YES];
    [topView addSubview:nickView];
    [nickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(oriView.mas_bottom);
        make.height.offset(45);
        make.bottom.mas_offset(0);
    }];
    
    
    UIView *botView = kView;
    botView.layer.cornerRadius = 8;
    botView.layer.masksToBounds = YES;
    botView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:botView];
    [botView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom).offset(15);
        make.left.right.equalTo(topView);
    }];
    
    UIView *emailView = [self itemViewWithTitle:@"用户邮箱".rh_localized content:self.usermodel.email showLine:YES canEdit:NO];
    [botView addSubview:emailView];
    [emailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(0);
        make.height.offset(45);
    }];
    
    UIView *contactView = [self itemViewWithTitle:@"联系方式".rh_localized content:self.usermodel.phonenumber showLine:YES canEdit:NO];
    [botView addSubview:contactView];
    [contactView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(emailView.mas_bottom);
        make.height.offset(45);
    }];
    
    UIView *adressView = [self itemViewWithTitle:@"用户地址".rh_localized content:self.usermodel.address showLine:NO canEdit:NO];
    [botView addSubview:adressView];
    [adressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(contactView.mas_bottom);
        make.height.offset(45);
        make.bottom.mas_offset(0);
    }];
    
    QMUIButton *sureBtn = [[QMUIButton alloc]init];
    [sureBtn setTitle:@"确认".rh_localized forState:UIControlStateNormal];
    [sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [sureBtn setCornerRadius:8];
    [sureBtn setBackgroundColor:CMainColorGreen];
    [sureBtn addTarget:self action:@selector(remarkAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(kSafe_bottom>0?-kSafe_bottom:-15);
        make.height.offset(50);
    }];
}

-(UIView*)itemViewWithTitle:(NSString*)title content:(NSString *)content showLine:(BOOL)showLine canEdit:(BOOL)canEdit
{
    UIView *bgView = kView;
    QMUILabel *titleLab = [QMUILabel labelWithText:title font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8)];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.numberOfLines = 2;
    titleLab.adjustsFontSizeToFitWidth = YES;
    titleLab.minimumScaleFactor = 0.8;
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.centerY.mas_offset(0);
        make.width.lessThanOrEqualTo(@(100));
    }];
    
    QMUITextField *textField = [[QMUITextField alloc]init];
    textField.textColor = kCOLOR_HEX(@"#171A1D");
    textField.placeholderColor = kCOLOR_HEX_A(@"#171A1D", 0.25);
    textField.userInteractionEnabled = canEdit;
    textField.font = kSYSTEMFONT(14);
    [bgView addSubview:textField];
    textField.text = content;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    if (canEdit) {
        self.nickTF = textField;
    }
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab.mas_right).offset(5);
        make.right.mas_offset(-0);
        make.centerY.mas_offset(0);
        make.height.offset(40);
    }];
    if (showLine) {
        UIView *line = kView;
        line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
        [bgView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab);
            make.bottom.mas_offset(0);
            make.right.equalTo(textField);
            make.height.offset(1);
        }];
    }
    
    if (canEdit) {
        textField.placeholder = @"请输入新用户名称".rh_localized;
         
    }
    return bgView;
}

-(void)remarkAction
{
    if (_nickTF.text.length == 0) {
        [QMUITips showInfo:@"请输入新用户名称".rh_localized];
        return;
    }
    NSDictionary *para = @{ @"userId":@(self.usermodel.userId),  // 下级用户id
                            @"superAdminId":@([PrivateMange share].currentUser.userId),  // 上级用户id
                            @"remark":self.nickTF.text // 备注信息
    };
    [QMUITips showLoadingInView:kApplication.keyWindow];
    [BaseModel startWithCompletionAPIKey:H_subUserRemark argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            self.usermodel.remarkName = self.nickTF.text;
            
            [QMUITips showSucceed:@"成功".rh_localized inView:self.view hideAfterDelay:1];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter]postNotificationName:UserModifyRemarkName object:self.usermodel];
            });
        }
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
        }];
    
    
}

@end
